package scau.mis4.chanyulin.atm.physical;

import scau.mis4.chanyulin.atm.base.common.CODE;
import scau.mis4.chanyulin.atm.base.system.ConfigConst;

/**
 * Created by chanyulin on 2018/5/24.
 * 出钞机
 */
public class Cashier {

    // 剩余钞票金额
    private int moneyLeft;
    // 已出钞金额
    private static int moneyEjected;
    // 存款过程中暂存的钞票
    private int moneyCache;


    public Cashier() {
        this.moneyLeft = ConfigConst.CASHIER_MONEY_LEFT;
    }

    /**
     * 出钞
     * @return
     */
    public int ejectMoney(int moneyNeed) {

        if (moneyLeft < moneyNeed) return CODE.ERR_OUT_OF_CASH;
        if (moneyEjected > 0) recoverMoney();

        moneyLeft -= moneyNeed;
        moneyEjected = moneyNeed;
        return CODE.MSG_SUCCESS;

    }

    /**
     * 取走钞票
     * @return
     */
    public static int takeMoney() {
        int moneyToTake = moneyEjected;
        moneyEjected = 0;
        return moneyToTake;
    }

    /**
     * 回收钞票
     */
    public void recoverMoney() {
        if (moneyEjected > 0) {
            moneyLeft += moneyEjected;
            moneyEjected = 0;
        }
    }

    /**
     * 放入钞票
     * @param moneyPut
     */
    public static void putMoney(int moneyPut) {
        moneyEjected += moneyPut;
    }

    /**
     * 清点钞票
     * @return
     */
    public int countMoneyCache() {
        // 模拟清点过程
        int i = 0;
        for (; i < moneyCache; i++);
        return i;
    }

    /**
     * 将放入的钞票转入暂存区
     * @return
     */
    public int moveMoneyToCache() {
        if (moneyEjected == 0 || moneyEjected % 100 != 0) return CODE.ERR_NUMBER_OUT_OF_RANGE;

        moneyCache += moneyEjected;
        moneyEjected = 0;
        return CODE.MSG_SUCCESS;
    }

    /**
     * 将暂存区的钞票弹出
     * @return
     */
    public int ejectMoneyCache() {
        if (moneyCache == 0) return CODE.ERR_NUMBER_OUT_OF_RANGE;

        moneyEjected = moneyCache;
        moneyCache = 0;
        return CODE.MSG_SUCCESS;
    }

    /**
     * 存入钞票
     * @return
     */
    public int saveMoney() {
        if (moneyCache == 0) return CODE.ERR_NUMBER_OUT_OF_RANGE;

        moneyLeft += moneyCache;
        moneyCache = 0;
        return CODE.MSG_SUCCESS;
    }

    /**
     * 获取已出钞金额
     * @return
     */
    public static int getMoneyEjected() {
        return moneyEjected;
    }

    /**
     * 获取暂存的钞票金额
     * @return
     */
    public int getMoneyCache() {
        return moneyCache;
    }


}
